package controllers

import (
	"github.com/astaxie/beego"
	"Fblog/models"
	"github.com/astaxie/beego/logs"
	"strings"
)

//首页
type CategoryController struct {
	beego.Controller
}

// @router /category [get]
func (c *CategoryController) Get() {
	c.TplName = "category.html"

	c.Data["IsCategory"] = true
	c.Data["Dir"] = beego.AppConfig.String("Dir")
}

// @router /category [post]
func (c *CategoryController) Post() {
	title := strings.TrimSpace(c.Input().Get("Title"))

	if title == "" {
		return
	}
	err := models.AddCategory(title)

	ret := Ret{}
	if err != nil {
		logs.Info(err)
		ret.Error = 201
		ret.Message = "添加失败"
	} else {
		ret.Error = 200
		ret.Message = "添加成功"
	}

	c.Data["json"] = ret

	c.ServeJSON()
	return
}

// @router /category/all [get]
func (c *CategoryController) GetAll() {
	page := models.Page{}

	if err := c.ParseForm(&page); err != nil {
		logs.Info(err)
	}

	c.Data["json"], _ = models.GetAllCategories(page)

	c.ServeJSON()
	return
}

type Ret struct {
	Error   int `json:"error"`
	Message string `json:"message"`
}
